GBML manual / Lists

list_create

Makes an empty list that can hold up to capacity items. example is any value of the kind it will hold - list_create(16, 0) is a list, list_create(8, "") a list - and is never put in the list. Write the example as the kind you want: 0 is whole numbers and 0.0 is decimals, and a decimal does not fit in a list of whole numbers later (a `/// @type {list}` comment above the line makes it decimals whatever the example is). capacity must be a whole number written right there in the code.

Syntax

list_create(capacity, example) -> list<T>

Arguments

NameType
capacityint
exampleelement

Returns

list<T>

Description

Makes an empty list that can hold up to capacity items. list_create(16, 0) is a list of whole numbers with room for 16.

capacity is the most the list will ever hold. It has to be a number you type, like 16, not a variable. A GBA cannot hand out memory while a game runs - every list is given its room when the game is built, and a variable does not have a value yet at that point. If you use one, the compiler says so and explains it.

example tells GBML what kind of thing goes in the list. It can be any value of that kind: 0 for whole numbers, 0.0 for decimals, "" for text, true for yes-or-no, or a struct such as { x: 0, y: 0 }. The list itself starts empty. The example is never put in it, and it never runs - it is only read to learn the kind. That is the one difference from array_create, where the second value really does fill every slot. If you want a list that starts full of one value, make it and then call list_fill.

The example decides what kind of list it is, so write it as the kind you want. list_create(13, 0) is a list of whole numbers, and list_create(13, 0.0) is a list of decimals. A whole number fits into a list of decimals, but a decimal does not fit into a list of whole numbers. So if the list will ever hold a decimal - an answer from lerp, a division, a speed - use 0.0. If you write 0 and later put a decimal in - items[i] = lerp(...), items[i] += 0.5, list_add(items, 1.5) - GBML refuses the project when it is checked. The error points at the line that wrote the decimal, and it also says which list_create made the list hold whole numbers, even when that line is in a different event, so you know where to fix it: put 0.0 there. A /// @type {list<real>} comment above the line, or var items: list<real, 13> = list_create(13, 0);, also makes it a list of decimals whatever the example is.

It sets the size of the variable it is given to, so it goes on the right-hand side of the line that creates the variable and nowhere else, not inside another expression. var items = list_create(16, 0); in an event, or items = list_create(16, 0); in Create for an instance variable.

Leave the size out and you get 8. A list that is never given a size - no list_create, no /// @capacity, no number in its type - holds 8 items.

Three ways to say the same thing

These make exactly the same list. Use whichever reads best to you; none of them is going away.

HowWhat you write
a comment/// @type {list<int>} then /// @capacity 16 then var items = [];
the type written outvar items: list<int, 16> = [];
a functionvar items = list_create(16, 0);

The comment form is the only one that is not normal code; see Writing a type out for why that matters.

Use the written-out type when the list is going to hold references to instances, such as var enemies: list<ref<obj_enemy>, 16> = [];. There is no value you could give as the example for "a reference to an enemy that does not exist yet", and nothing is refused because it does not say what kind of thing it is.

Example

var names = list_create(4, "");        // an empty list of text, room for 4
list_add(names, "Ada");
list_add(names, "Grace");
var how_many = list_length(names);     // 2 - the "" was never in the list
var points = list_create(8, { x: 0, y: 0 });   // an empty list of points
list_add(points, { x: 3, y: 4 });

On the GBA

Compiles to an empty fixed-size vector of `capacity` items - the same storage `/// @type {list<T>}` + `/// @capacity capacity` and `var x: list<T, capacity> = [];` give, so the three spellings are interchangeable and this one is not slower or bigger. Leave the size out and a list holds 8. The size has to be a plain number because it becomes a C++ template parameter: the Game Boy Advance has no heap to hand out memory while the game runs, so every container is sized when the ROM is built (anything else is refused). It must be the whole right-hand side of a declaration, because that is the one place a size can be attached to a variable. The example is only read for its kind - it is never evaluated, never stored and never put in the container.

See also: list_create_filllist_insertlist_addlist_filllist_copyarray_createstack_createqueue_createset_createmap_createListsWriting a type out, instead of in a commentData containers